﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
    public partial class wfAreaMnt : EditPage
    {
      
        public const int _GRDLIST_IDCONREMU = 3;
        public const int _GRDLIST_STINHABILITA = 6;
        public const int _GRDLIST_STANULADO = 7;
       
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                this.LoadInPostBack();
                return;
            }
            this.LoadData();
        }

        public override void LoadInPostBack()
        {
            this.RegisterGeneralScripts();
        }

        public override void LoadInitialSettings()
        {
           this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
           this.SetInitialTitle("Areas");
        }

        public override void LoadListControls()
        {
            ETablaGenDet objEDet = new ETablaGenDet();
            ECCosto objECCosto = new ECCosto();

            EUNegocio objEUNeg = new EUNegocio();
            MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
            IBOQuery objBOUNego = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.UNegocio));
            IBOQuery objBOCCosto = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.CCosto));

            DataTable dt = objBOI.GetDetailByList(objEDet);
            objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            dt = objBOUNego.GetList(objEUNeg);
            WebHelper.LoadListControl(ddlidUNegocio, dt, "dsUNegocio", "idUNegocio", true, false);

        }

        public override void DataToControls()
        {
            EArea objE = new EArea();
            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Area));
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdArea = Request.QueryString["idArea"];
            objE = (EArea)objBO.GetByKey(objE);


            if (objE != null)
            {

                WebHelper.EntityToControl(objE, "IdArea", txtidArea);

                WebHelper.EntityToControl(objE, "IdUNegocio", ddlidUNegocio);

                ddlidCCosto.Items.Clear();
                EUNegocio objEUNeg = new EUNegocio();
                MGBOQry.IUNegocio objBOUNego = (MGBOQry.IUNegocio)WCFHelper.GetObject<MGBOQry.IUNegocio>(typeof(MGBOQry.UNegocio));
                objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                objEUNeg.IdUNegocio = objE.IdUNegocio;
                DataTable dtA = objBOUNego.GetCCostoXUNegocio(objEUNeg);
                WebHelper.LoadListControl(ddlidCCosto, dtA, "dsCCosto", "idCCosto", true, true);

                WebHelper.EntityToControl(objE, "IdCCosto", ddlidCCosto);
                WebHelper.EntityToControl(objE, "DsNombre", txtdsNombre);
                WebHelper.EntityToControl(objE, "DsDescrip", txtdsDescrip);

            }

                //EPlanillaDet objEPlaDet = new EPlanillaDet();

                //RHBOQry.Planilla objBOPerC = (RHBOQry.Planilla)WCFHelper.GetObject<RHBOQry.Planilla>(typeof(RHBOQry.Planilla));
                //objEPlaDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                //objEPlaDet.IdPlanilla = Request.QueryString["idPlanilla"];
                //DataTable dt = objBOPerC.GetByParentKeyPlaniDet(objEPlaDet);
                  
                //this.GridSetDataBinding(grdList, dt);
                //this.GridSort(grdList, "nuSecuen", SortDirection.Ascending);

       }

        public override void RegisterGeneralScripts()
        {
            txtidArea.Attributes.Add("readonly", "true");

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("function validateEditForm(key){");
 
            sb.AppendLine("var varRpta = isEmptyControl('" + this.txtdsNombre.ClientID + "','Ingrese Descripcion del Area');");
            sb.AppendLine("if(varRpta==true)");
            sb.AppendLine("return !varRpta;");
            //sb.AppendLine("var varRpta = isEmptyControl('" + this.txtnuProcAnual.ClientID + "','Ingrese Numero de Proceso Anual');");
            //sb.AppendLine("if(varRpta==true)");
            //sb.AppendLine("return !varRpta;");

            sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
            sb.AppendLine("}");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
        }

        public void RegisterEditPopupScripts()
        {
            //StringBuilder sb = new StringBuilder();

            //sb.AppendLine("var varRet = modalDialog('../RH/wfConcepRemu.aspx',725,400);");
            //sb.AppendLine("if(varRet==null)");
            //sb.AppendLine("return false;");
            //sb.AppendLine("ctl = document.getElementById('" + this.hdnidConRemu.ClientID + "');");
            //sb.AppendLine("ctl.value = varRet[0];");
            //sb.AppendLine("ctl = document.getElementById('" + this.txtidConRemu.ClientID + "');");
            //sb.AppendLine("ctl.value = varRet[1];");
            //sb.AppendLine("return false;");
            //this.txtidConRemu.OnClientClick = sb.ToString();

            //ScriptManager.RegisterStartupScript(this, this.GetType(), "", sb.ToString(), true);
          
            //StringBuilder sb2 = new StringBuilder();

            //sb2.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
            ////sb.AppendLine("debugger;");
            //if (this.GridEditAction == GridEditAction.AddNew)
            //{
            //   sb2.AppendLine("varRpta = gridFindCellValueInRow('" + grdList.ClientID + "',new Array('" + _GRDLIST_IDCONREMU + "'), new Array(document.getElementById('" + this.hdnidConRemu.ClientID + "').value),'Ya existe el concepto de Remuneracion');");
            //   sb2.AppendLine("if(varRpta==true)");
            //   sb2.AppendLine("return !varRpta;");
            //}

            //sb2.AppendLine("varRpta = isEmptyControl('" + this.txtnuSecuen.ClientID + "','Ingrese Numero de Secuencia.');");
            //sb2.AppendLine("if(varRpta==true)");
            //sb2.AppendLine("return !varRpta;");
            //sb2.AppendLine("return true;}");

            //ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb2.ToString(), true);

            //this.bttOkPopup.OnClientClick = "return validate" + bttOkPopup.ID + "();";
            //this.bttCancelPopup.OnClientClick = "$find('" + this.mdlPopup.ClientID + "').hide();return false;";

        }
       
        protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
        {
           if (e.Item == null)
              return;

           switch (e.Item.Value)
           {
              case ConstMenuToolbarValue.BUTTON_SAVE:
              case ConstMenuToolbarValue.BUTTON_SAVENEW:
                 if (this.FormEditAction != FormEditAction.Delete)
                    this.SaveData(e.Item.Value);
                 else
                    this.DeleteData();
                 break;

              case ConstMenuToolbarValue.BUTTON_EXIT:
                 this.Response.Redirect("wfAreaBnd.aspx");
                 break;
           }

        }

        public override void SaveData(String key)
        {
            if (this.ValidateData() == false)
                return;

            IEntityBase objE = (IEntityBase)this.CreateEntitySet();

            IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Area));

            String[] strRet = objPro.UpdateData(objE);
            String strMsg = "";

            switch (this.FormEditAction)
            {

                case FormEditAction.AddNew:
                case FormEditAction.Copy:
                    strMsg = "Se ha registrado el Area : " + strRet[0].ToString();
                    break;
                default:
                    strMsg = "Se ha modificado el Area : " + txtidArea.Text + "  " + txtdsNombre.Text;
                    break;
            }
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfAreaBnd.aspx");
         
        }
       
        //protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
        //{
        //    this.EditRowIndex = Convert.ToInt32(e.CommandArgument);

        //    switch (e.CommandName)
        //    {
        //        case ConstGridRowCommand.GRIDEDIT:
        //            this.lblTitlePopup.Text = "Detalle Planilla (Editar)";
        //            this.GridEditAction = GridEditAction.Edit;
        //            this.mdlPopup.Show();
        //            this.LoadDataEditPopup(grdList, this.EditRowIndex);
        //            this.RegisterEditPopupScripts();
        //            break;
        //        case ConstGridRowCommand.GRIDDELETE:
        //            this.lblTitlePopup.Text = "Detalle Planilla (Dar de Baja)";
        //            this.GridEditAction = GridEditAction.Delete;
        //            this.mdlPopup.Show();
        //            this.LoadDataEditPopup(grdList, this.EditRowIndex);
        //            this.RegisterEditPopupScripts();
        //            break;
        //    }
        //}

        //protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
        //        { e.Row.ForeColor = System.Drawing.Color.Red; }

        //        CheckBox chk = (CheckBox)e.Row.FindControl("chkstDesHabilita");
        //        if (e.Row.Cells[_GRDLIST_STINHABILITA].Text == ConstFlagEstado.ACTIVADO)
        //        {
        //           chk.Checked = true;
        //        }

        //    }

        //}
        
        //protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
        //{
        //    if (e.Item == null)
        //        return;

        //    switch (e.Item.Value)
        //    {
        //        case ConstMenuToolbarValue.BUTTON_NEW:
        //            this.GridEditAction = GridEditAction.AddNew;
        //            this.lblTitlePopup.Text = "Planilla (Nuevo)";
        //            this.mdlPopup.Show();
        //            this.ClearEditPopup();
        //            this.LoadDataEditPopup(grdList, -1);
        //            this.RegisterEditPopupScripts();
        //            break;
        //    }

        //}

        //protected void bttOkPopup_Click(object sender, EventArgs e)
        //{

        //    DataTable dt = (DataTable)this.DataSource;
        //    DataRow dr = null;

        //    if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
        //    {
               
        //        dr = dt.NewRow();
        //    }
        //    else
        //    {
        //        dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
        //    }

        //    if (this.GridEditAction != GridEditAction.Delete)
        //    {
        //        dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
        //        dr["IdPlanilla"] = Request.QueryString["IdPlanilla"];
        //        dr["idConRemu"] = this.hdnidConRemu.Value.ToString();
        //        dr["dsConRemu"] = this.txtidConRemu.Text.ToString();
        //        dr["nuSecuen"] = this.txtnuSecuen.Text.ToString();

        //        if (chkstHabilitado.Checked)
        //           dr["stInhabilita"] = ConstFlagEstado.ACTIVADO;
        //        else
        //           dr["stInhabilita"] = ConstFlagEstado.DESACTIVADO;

        //        dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;

        //    }
        //    else
        //    {
        //        dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
        //    }

        //    if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
        //    {
        //        dt.Rows.Add(dr);
        //    }


        //    this.GridSetDataBinding(grdList, dt);
        //}
       
        //public override void LoadDataEditPopup(GridView grid, int rowIndex)
        //{
        //    this.ClearEditPopup();
        //    this.LockControlsPopup();

        //    DataRow dr = this.GridGetRowFromDataKey(this.DataSource, grdList, rowIndex);

        //    if (dr == null)
        //        return;

        //    WebHelper.RowToControl(dr, "idConRemu", hdnidConRemu);
        //    WebHelper.RowToControl(dr, "dsConRemu", txtidConRemu);
        //    WebHelper.RowToControl(dr, "nuSecuen", txtnuSecuen);
        //    WebHelper.RowToControl(dr, "stInhabilita", chkstHabilitado);

        //}

        //public new void ClearEditPopup()
        //{
        //   this.chkstHabilitado.Checked = false;
        //   this.txtnuSecuen.Text = "0";
        //   this.txtidConRemu.Text = "";
        //   this.hdnidConRemu.Value = "";

        //}

        public void LockControlsPopup()
        {
            //switch (this.GridEditAction)
            //{
            //    case GridEditAction.AddNew:
            //    case GridEditAction.Edit:
            //        this.txtfeBajaDH.Enabled = false;
            //        this.ddlidMotivoBajaDH.Enabled = false;
            //        break;
            //    case GridEditAction.Delete:
            //        this.txtfeBajaDH.Enabled = true;
            //        this.ddlidMotivoBajaDH.Enabled = true;
            //        this.txtdsApePaternoDH.Enabled = false;
            //        this.txtdsApeMaternoDH.Enabled = false;
            //        this.txtdsNombresDH.Enabled = false;
            //        this.ddlidTipoDocDH.Enabled = false;
            //        this.txtnuDocDH.Enabled = false;
            //        this.txtfeNacimientoDH.Enabled = false;
            //        this.ddlidGeneroDH.Enabled = false;
            //        this.ddlidVincFamDH.Enabled = false;
            //        this.ddlidVincFamDH.Enabled = false;
            //        this.ddlidTipoDocVincFamDH.Enabled = false;
            //        this.txtnuDocVincFamDH.Enabled = false;
            //        this.txtfeConcepDH.Enabled = false;
            //        this.txtnuCodLDN.Enabled = false;
            //        this.txtnuTelefonoDH.Enabled = false;
            //        this.txtdsEmailDH.Enabled = false;
            //        this.txtdsUbigeo.Enabled = false;
            //        this.ddlidTipoVia.Enabled = false;
            //        this.txtdsNombreVia.Enabled = false;
            //        this.txtnuVia.Enabled = false;
            //        this.txtnuDpto.Enabled = false;
            //        this.txtnuInterior.Enabled = false;
            //        this.txtdsManzana.Enabled = false;
            //        this.txtdsLote.Enabled = false;
            //        this.txtdsKilometro.Enabled = false;
            //        this.txtdsBlock.Enabled = false;
            //        this.txtdsEtapa.Enabled = false;
            //        this.ddlidTipoZona.Enabled = false;
            //        this.txtdsNombZona.Enabled = false;
            //        this.txtdsReferen.Enabled = false;
            //        this.txtdsUbigeo2.Enabled = false;
            //        this.ddlidTipoVia2.Enabled = false;
            //        this.txtdsNombreVia2.Enabled = false;
            //        this.txtnuVia2.Enabled = false;
            //        this.txtnuDpto2.Enabled = false;
            //        this.txtnuInterior2.Enabled = false;
            //        this.txtdsManzana2.Enabled = false;
            //        this.txtdsLote2.Enabled = false;
            //        this.txtdsKilometro2.Enabled = false;
            //        this.txtdsBlock2.Enabled = false;
            //        this.txtdsEtapa2.Enabled = false;
            //        this.ddlidTipoZona2.Enabled = false;
            //        this.txtdsNombZona2.Enabled = false;
            //        this.txtdsReferen2.Enabled = false;
            //        break;
            //}
        }


        protected void ddlidUNegocio_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlidCCosto.Items.Clear();
            EUNegocio objEUNeg = new EUNegocio();
            MGBOQry.IUNegocio objBOUNego = (MGBOQry.IUNegocio)WCFHelper.GetObject<MGBOQry.IUNegocio>(typeof(MGBOQry.UNegocio));
            objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEUNeg.IdUNegocio = ddlidUNegocio.SelectedItem.Value;
            DataTable dt = objBOUNego.GetCCostoXUNegocio(objEUNeg);
            WebHelper.LoadListControl(ddlidCCosto, dt, "dsCCosto", "idCCosto", true, true);
        }


        //public override void DeleteData()
        //{
        //    IEntityBase objE = (IEntityBase)this.CreateEntitySet();
        //    IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
        //    objPro.DeleteData(objE);

        //    this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el Personal  : " + txtidPersonal.Text, "wfPersonalBnd.aspx");

        //}

        public override Object CreateEntitySet()
        {
            string strIdEstado = "";
            EArea objE = new EArea();

            if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
                objE.EntityState = EntityState.Added;
            else
            {
                IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Area));
                objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
                objE.IdArea = txtidArea.Text;
                objE = (EArea)objBO.GetByKey(objE);

                strIdEstado = objE.IdEstado;
                if (this.FormEditAction == FormEditAction.Edit)
                    objE.EntityState = EntityState.Modified;
                if (this.FormEditAction == FormEditAction.Delete)
                    objE.EntityState = EntityState.Deleted;

            }

            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

            WebHelper.ControlToEntity(txtidArea, objE, "IdArea");
            WebHelper.ControlToEntity(txtdsNombre, objE, "DsNombre");
            WebHelper.ControlToEntity(ddlidUNegocio, objE, "IdUNegocio");
            WebHelper.ControlToEntity(ddlidCCosto, objE, "IdCCosto");
            WebHelper.ControlToEntity(txtdsDescrip, objE, "DsDescrip");


            DataTable dt = (DataTable)this.DataSource;
           

            IList<EArea> colE = new List<EArea>();

            for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
            {
                DataTable dtChange = null;
                if (intTypeUpd == 0)
                    dtChange = dt.GetChanges(DataRowState.Added);
                else
                    dtChange = dt.GetChanges(DataRowState.Modified);


                if (dtChange != null)
                {
                    foreach (DataRow drDet in dtChange.Rows)
                    {
                        EArea objEDet = DataHelper.CopyDataRowToEntity<EArea>(drDet, typeof(EArea));

                        if (drDet.RowState == DataRowState.Added)
                            objEDet.EntityState = EntityState.Added;

                        if (drDet.RowState == DataRowState.Modified)
                        {
                            if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                                objEDet.EntityState = EntityState.Modified;
                            else
                                objEDet.EntityState = EntityState.Deleted;
                        }

                        colE.Add(objEDet);
                    }
                }
            }

            EAreaSet objESet = new EAreaSet();

            //objESet.Area = objE;
            objESet.ColArea = colE; 

            return objESet;
        }

    }
}
